package com.yinhe.music.yhmusic.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yinhe.music.common.utils.DisplayUtil;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.album.TabActivity;
import com.yinhe.music.yhmusic.cache.CacheManager;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.instrument.InstrumentActivity;
import com.yinhe.music.yhmusic.instrument.InstrumentBean;
import com.yinhe.music.yhmusic.model.HomeDataItem;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.model.MvsInfo;
import com.yinhe.music.yhmusic.model.RadioListInfo;
import com.yinhe.music.yhmusic.model.SingerList;
import com.yinhe.music.yhmusic.model.SongMenuList;
import com.yinhe.music.yhmusic.radio.RadioListActivity;
import com.yinhe.music.yhmusic.service.PlayService;
import com.yinhe.music.yhmusic.singer.SingerActivity;
import com.yinhe.music.yhmusic.songmenu.SongMenuListActivity;
import com.yinhe.music.yhmusic.video.VideoActivity;
import com.yinhe.music.yhmusic.widget.SpacesItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by hasee on 2017/8/15.
 */

public class HomePageMusicAdapter extends BaseAdapter {


    private List<HomeDataItem> dataItemList = new ArrayList<>();

    protected PlayService mService;

    public interface HomePageMusicAdapterListener {
        /**
         * 歌曲列表点击
         *
         * @param position  位置
         * @param musicList 列表信息
         */
        void didClickAblumItem(int position, List<Music> musicList);

        /**
         * 歌曲-更多按钮点击
         *
         * @param music 选中的歌曲
         */
        void didClickMoreAblumItem(Music music);

        /**
         * mv列表点击
         *
         * @param info mv信息
         */
        void didClickMvItem(MvsInfo info);

        /**
         * 歌单item点击
         *
         * @param item 歌单信息
         */
        void didClickSongMenuItem(SongMenuList item);

        /**
         * 电台item点击
         *
         * @param info 点击电台信息
         */
        void didClickRadioItem(RadioListInfo info);

        /**
         * 歌手item点击
         *
         * @param singer 歌手信息
         */
        void didClickSingerItem(SingerList singer);

        /**
         * 乐器item点击
         *
         * @param instrument 乐器信息
         */
        void didClickInstrumentItem(InstrumentBean.InstrumentListBean instrument);
    }

    private HomePageMusicAdapterListener mListener;
    private Context mContext;

    public HomePageMusicAdapter(Context context, HomePageMusicAdapterListener listener) {
        mListener = listener;
        mContext = context;
    }

    public void clearData() {
        if (dataItemList != null) {
            dataItemList.clear();
        }
    }

    public void addData(HomeDataItem item) {
        if (dataItemList != null && item != null) {
            dataItemList.add(item);
        }
    }

    public void updateData(List<HomeDataItem> items) {
        if (items != null) {
            dataItemList.clear();
            dataItemList.addAll(items);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return dataItemList.size();
    }

    @Override
    public HomeDataItem getItem(int i) {
        return dataItemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        int type = getItemViewType(i);

        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.home_page_recycler_layout, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.recyclerView = view.findViewById(R.id.recycler_view);
            viewHolder.headerView = view.findViewById(R.id.head_text);
            viewHolder.moreView = view.findViewById(R.id.more_text);
            view.setTag(viewHolder);

            int leftRight = DisplayUtil.dip2px(mContext, 20);
            int topBottom = DisplayUtil.dip2px(mContext, 0);

            switch (type) {
                case HomeDataItem.TYPE_MUSIC:
                case HomeDataItem.TYPE_MUSIC_LIST:
                    viewHolder.recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mContext));
                    break;
                case HomeDataItem.TYPE_MV:
                    viewHolder.recyclerView.setLayoutManager(new GridLayoutManager(mContext, 2));
                    viewHolder.recyclerView.addItemDecoration(new SpacesItemDecoration(leftRight, topBottom));
                    break;
                case HomeDataItem.TYPE_RADIO:
                    viewHolder.recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
                    viewHolder.recyclerView.addItemDecoration(new SpacesItemDecoration(leftRight, topBottom));
                    break;
                case HomeDataItem.TYPE_SONG_MENU:
                    viewHolder.recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
                    viewHolder.recyclerView.addItemDecoration(new SpacesItemDecoration(leftRight, topBottom));
                    break;
                case HomeDataItem.TYPE_SINGER:
                case HomeDataItem.TYPE_INSTRUMENT:
                    viewHolder.recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
                    break;
                default:
                    break;

            }
            viewHolder.recyclerView.setFocusable(false);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        final HomeDataItem item = dataItemList.get(i);

        switch (item.getType()) {
            case HomeDataItem.TYPE_MUSIC:
                viewHolder.headerView.setText(R.string.recommend_song);
                viewHolder.recyclerView.setAdapter(item.getAlbumAdapter());
                item.getAlbumAdapter().setNewData(item.getMusicList());
                item.getAlbumAdapter().setOnItemClickListener((adapter, v, position) -> {
                    if (mListener != null) {
                        mListener.didClickAblumItem(position, item.getMusicList());
                    }
                });

                item.getAlbumAdapter().setOnItemChildClickListener((adapter, v, position) -> {
                    List<Music> musicList = item.getMusicList();
                    if (musicList.isEmpty()) {
                        return;
                    }
                    Music music = musicList.get(position);
                    if (mListener != null) {
                        mListener.didClickMoreAblumItem(music);
                    }
                });

                viewHolder.moreView.setOnClickListener((v) -> {
                    Intent intent;
                    if (item.getType() == HomeDataItem.TYPE_MUSIC) {
                        intent = new Intent(mContext, TabActivity.class);
                        intent.putExtra("type", IConstants.NEW_MUSIC_ACTIVITY);
                    } else {
                        intent = new Intent(mContext, InstrumentActivity.class);
                    }
                    mContext.startActivity(intent);
                });
                break;
            case HomeDataItem.TYPE_MV:
                viewHolder.headerView.setText(R.string.recommend_mv);
                viewHolder.recyclerView.setAdapter(item.getMvAdapter());
                item.getMvAdapter().setNewData(item.getMvList());
                item.getMvAdapter().setOnItemClickListener((adapter, v, position) -> {
                    MvsInfo mv = item.getMvList().get(position);
                    if (mListener != null) {
                        mListener.didClickMvItem(mv);
                    }
                });

                viewHolder.moreView.setOnClickListener((v) -> {
                    Intent intent = new Intent(mContext, VideoActivity.class);
                    intent.putExtra("type", IConstants.NEW_MORE_MV);
                    mContext.startActivity(intent);
                });
                break;
            case HomeDataItem.TYPE_RADIO: {
                viewHolder.headerView.setText(R.string.recommend_radio);
                viewHolder.recyclerView.setAdapter(item.getRadioAdapter());
                item.getRadioAdapter().setNewData(item.getRadioList());
                item.getRadioAdapter().setOnItemClickListener((adapter, v, position) -> {
                    RadioListInfo radio = item.getRadioList().get(position);
                    if (mListener != null) {
                        mListener.didClickRadioItem(radio);
                    }
                });

                viewHolder.moreView.setOnClickListener((v) -> {
                    Intent intent = new Intent(mContext, RadioListActivity.class);
                    mContext.startActivity(intent);
                });
            }
            break;
            case HomeDataItem.TYPE_SONG_MENU: {
                if (CacheManager.getInstance().getTypeLangId() == 1) {
                    viewHolder.headerView.setText("曲调流派");
                } else {
                    viewHolder.headerView.setText(R.string.recommend_menu);
                }
                viewHolder.recyclerView.setAdapter(item.getSongMenuAdapter());
                viewHolder.recyclerView.setFocusable(false);
                item.getSongMenuAdapter().setNewData(item.getSongmenuList());
                item.getSongMenuAdapter().setOnItemClickListener((adapter, v, position) -> {
                    SongMenuList songMenu = item.getSongmenuList().get(position);
                    if (mListener != null) {
                        mListener.didClickSongMenuItem(songMenu);
                    }
                });
                viewHolder.moreView.setOnClickListener((v) ->
                    mContext.startActivity(new Intent(mContext, SongMenuListActivity.class))
                );
            }
            break;
            case HomeDataItem.TYPE_INSTRUMENT:
                viewHolder.headerView.setText("乐器推荐");
                viewHolder.recyclerView.setAdapter(item.getInstrumentAdapter());
                viewHolder.recyclerView.setFocusable(false);
                item.getInstrumentAdapter().setNewData(item.getmInstrumentList());

                item.getInstrumentAdapter().setOnItemClickListener((adapter, v, position) -> {
                    InstrumentBean.InstrumentListBean listBean = item.getmInstrumentList().get(position);
                    if (mListener != null) {
                        mListener.didClickInstrumentItem(listBean);
                    }
                });

                viewHolder.moreView.setOnClickListener((v) -> {
                    Intent intent = new Intent(mContext, InstrumentActivity.class);
                    mContext.startActivity(intent);
                });
                break;
            case HomeDataItem.TYPE_SINGER: {
                viewHolder.headerView.setText("歌手推荐");
                viewHolder.recyclerView.setAdapter(item.getSingerAdapter());
                viewHolder.recyclerView.setFocusable(false);
                item.getSingerAdapter().setNewData(item.getmSingerList());
                item.getSingerAdapter().setOnItemClickListener((adapter, v, position) -> {
                    SingerList singer = item.getmSingerList().get(position);
                    if (mListener != null) {
                        mListener.didClickSingerItem(singer);
                    }
                });
                viewHolder.moreView.setOnClickListener((v) -> {
                    Intent it = new Intent(mContext, SingerActivity.class);
                    mContext.startActivity(it);
                });
                break;
            }
            default:
                break;
        }


        return view;
    }

    @Override
    public int getViewTypeCount() {
//        if (dataItemList != null && dataItemList.size() > 0) {
//            return dataItemList.size();
//        } else {
//            return 0;
//        }
        return 7;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < dataItemList.size()) {
            HomeDataItem item = dataItemList.get(position);
            return item.getType();
        }
        return 0;
    }

    public class ViewHolder {
        public TextView headerView;
        public TextView moreView;
        public RecyclerView recyclerView;
    }
}
